﻿using System;
using System.Linq;
using System.Collections.Generic;
using SoldiHive.Infrustructure.Extensions;

namespace SoldiHive.Model.Views
{
	[Serializable]
	public class SectorView
	{
		public Company Company { get; set; }
		public Quote StartQuote { get; set; }
		public Quote EndQuote { get; set; }
		public IList<Rating> Ratings { get; set; }
		public double AvarageRating { get; set; }

		public SectorView(Company company, Quote startQuote, Quote endQuote, IList<Rating> ratings)
		{
			this.Company = company;
			this.StartQuote = startQuote;
			this.EndQuote = endQuote;
			this.Ratings = ratings;

			this.AvarageRating = this.Ratings
				.Where(rating => rating.Rate.HasValue)
				.Select(raiting => raiting.Rate.Value)
				.Average();
		}

		public override string ToString()
		{
			var ratings = this.Ratings.Select(rating => rating.ToString());
			var ratingsString = string.Join(", ", ratings);
			var priceChange = Math.Abs(((this.EndQuote.Close / this.StartQuote.Open) - 1) * 100);

			var format = "{0} {1} {2} {3:0.0} [{4}] {5:Sector,Industry,SubIndustry}";
			var sectorViewString = string.Format(format,
				this.Company.Ticker.Align(4),
				string.Format("{0:Cap}", this.Company).Align(8),
				string.Format("{0}{1:0.00}%", this.EndQuote.Close >= this.StartQuote.Open ? "▲" : "▼", priceChange).Align(7),
				this.AvarageRating,
				ratingsString,
				this.Company);

			return sectorViewString;
		}
	}
}
